$(document).ready(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
	$("#form_flagStatus").combobox({

		onChange: function (obj,showID) {

			if($(this).combobox("getValue")==1){
				$("#islunbo").css('display','table-row'); 
			}else{
				$("#islunbo").css('display','none'); 
			}
		}

		});
});

$(window).resize(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
});

function onDblClickRow(rowIndex,rowData){
	window.open("admin/news/news_contentShow.action?news.newsId="+rowData.newsId, '', 'height=620, width=600, top='+(window.screen.availHeight-10-620)/2+', left='+(window.screen.availWidth-10-600)/2+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no'); 
}


/**
 * 条件资讯搜索
 */
function searchNews(){
	$("#news").datagrid("load",{
		"s_category":$("#search_category_name").combobox("getValue"),
		"s_lunboFlag":$("#search_lunboFlag").combobox("getValue"),
		"s_title":$("#search_title").val(),
		"s_status":$("#search_status").combobox("getValue"),
	});
}
/**格式化使用状态列，将传入值变成复选框模式*/
function newsStatusFormatter(value, rec, index){
	var checked=rec.status==1?"checked":"";
	 return "<input id='radio_newsStatus' name='news.status' type='checkBox' "+checked+"  disabled=\"disabled\"/>";
}

/**格式化使用状态列，将传入值变成汉字*/
function newsLunboFlagFormatter(value, rec, index){
	var lunboFlag=rec.lunboFlag?"轮播":"列表";
	 return lunboFlag;
}

/**格式化使用状态列，将传入值变成<a>标签*/
function newsSuolveFormatter(value, rec, index){
	var suolveImg = rec.imgURL != null ?"<a class='ml20' style='cursor:pointer;color: #2A2AFF;'   id='click_show_suolveImg' onclick=\"tanchupic('"+ rec.imgURL.substr(1)+"')\">[查看]</a>":"";
	 return suolveImg;
}

function newsLunboFormatter(value, rec, index){
	var lunboImg = rec.lunboImg != null ?"<a class='ml20' style='cursor:pointer;color: #2A2AFF;'   id='click_show_lunboImg' onclick=\"tanchupic('"+ rec.lunboImg.substr(1)+"')\">[查看]</a>":"";
	 return lunboImg;
}

/**格式化使用状态列，时间只显示年月日模式*/
function dateHHmmssFormatter(value, rec, index){
	 return value != null?value.replace("T"," "):"";
}

/**
 * 初始化资讯添加表单
 */
function form_news_init(){

	$("#form_newsId").val("");
	$("#form_title").textbox('setValue', "");
    $("#form_category_name").combobox('setValue', "");
	$("#form_summary").textbox('setValue', "");
	statusCombo("form_flagStatus", "empty");
	statusCombo("form_status", "empty");
	$("#islunbo").css('display','none'); 
	showImg(null ,"show_suolveImg");
	showImg(null ,"show_lunboImg");
	$("#form_imgURL").val("");
	$("#form_lunboImg").val("");
	editor.sync();
    // 设置HTML内容
	editor.html("");
	return true;
}


/**
 * 添加资讯详情
 */
function addNews(){
	
	$('#news_form_dialog').dialog({
        title: '资讯内容--添加',
        iconCls: 'icon-book_addresses_add',
    });
	
	//判断表格是否被选中，如果选中则取消选中
	var item=$('#news').datagrid('selectRow');
	if(!item==null){
		$("#news").datagrid("unselectRow", item);
	}
	
	//初始化表单值
	if(!form_news_init()){
		return;
	}

	 $('#news_form').form({
	 url: 'admin/news/news_saveNews.action',
	 onSubmit: function() {
	 return $(this).form('validate');
	 },
	 success: function(data) {
	 var json = JSON.parse(data);
	 if (json.result == "Success") {
	 $('#news_form_dialog').dialog('close');
	 $.messager.alert('提示', json.message, 'info');
	 $("#news").datagrid("load");
	 } else {
	 $.messager.alert('错误', json.message, 'error');
	 }
	 }
	    });
    $('#news_form_dialog').dialog('open');
	
}

/**
 * 修改资讯详情
 */
function modifyNews(){
	
	$('#news_form_dialog').dialog({
        title: '资讯内容--修改',
        iconCls: 'icon-book_addresses_edit',
    });
	
	//判断表格是否被选中，如果选中则取消选中
	var item="";
	try{
		item=$('#news').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要修改的详情项！', 'error');
		return;
	}
	form_news_init();
	//初始化表单值
	$.post("admin/news/news_newsModifyInfo.action?news.newsId=" + item.newsId,
	        function(data) {
				
			$("#form_newsId").val(data.news.newsId);
	        $("#form_category_name").combobox('setValue', data.news.categoryId);
	        //$("#form_category_name").combobox('setText', data.news.category.name);
	        $("#form_title").textbox('setValue', data.news.title);
	        //有缩略图时添加显示效果的<a>标签
	        showImg(data.news.imgURL,"show_suolveImg");
	        
	        //轮播checkbox赋初始值
	        statusCombo("form_flagStatus", data.news.lunboFlag);
	       if(data.news.lunboFlag){
	    	   $("#islunbo").css('display','table-row'); 
		   	}else{
		   		$("#islunbo").css('display','none'); 
		   	}
	        
	      //有轮播图时添加显示效果的<a>标签
	        showImg(data.news.lunboImg,"show_lunboImg");
	        
	        $("#form_summary").textbox('setValue', data.news.summary);
	        editor.sync();
	     // 设置HTML内容
	        editor.html(data.news.content);
	      //显示combobox赋初始值
	        statusCombo("form_status", data.news.status);
	    });
	
	
    $('#news_form').form({
        url: 'admin/news/news_updateNews.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#news_form_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
    			$("#news").datagrid("load");
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#news_form_dialog').dialog('open');
	
}

/**
 * 删除资讯详情
 */
function removeNews(){
	
	var item="";
	try{
		item=$('#news').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要删除的详情项！', 'error');
		return;
	}
	
	$.messager.confirm("操作提示", "您确定要执行删除操作吗？", function (data) {  
		if (data) {
	$.ajax({
		data:{
			"news.newsId":item.newsId
		},
		url:"admin/news/news_removeNews.action",
		type:'POST',
		success:function(data){
			 var json = data;
	            if (json.result == "Success") {
	                $.messager.alert('提示', json.message, 'info');
	                $("#news").datagrid("load");
	            } else {
	                $.messager.alert('错误', json.message, 'error');
	            }
		}
	});
}
	});
}


//function isLunBo(obj ,showID){
//	alert($(obj).combobox("getValue"));
//	if($(obj).combobox("getValue")==1){
//		$("#"+showID).css('display','table-row'); 
//	}else{
//		$("#"+showID).css('display','none'); 
//	}
//}

function submitDataForm() {
	
	if($('#form_category_name').combobox('getValue') == ""){
		 $.messager.alert('错误', '请选择类别', 'error');
		 return false;
	}
	if($('#form_status').combobox('getValue') == ""){
		 $.messager.alert('错误', '请选择显示状态', 'error');
		 return false;
	}
	if($('#form_flagStatus').combobox('getValue') == ""){
		 $.messager.alert('错误', '请选择展示类型', 'error');
		 return false;
	}
	if(editor.html() == ""){
		 $.messager.alert('错误', '请填写内容', 'error');
		 return false;
	}
	
	$('#news_form').submit();
}

function form_close (){
	$('#news_form_dialog').dialog('close');
}

/**
 * 用于ComboBox赋初始值
 * 
 * @param ComboBoxId
 *            下拉框ID
 * @param statusData
 *            初始状态
 */
/*function statusCombo(ComboBoxId, statusData){
	if(statusData =="empty"){
		$("#" + checkId).combobox("setValue","");
	}else{
		if(statusData){
			$("#" + checkId).combobox("setValue","1");
		}else{
			$("#" + checkId).combobox("setValue","0");
		}
	}
}*/

/**
 * 用于编辑显示图片的<a>标签生成
 * 
 * @param imgShowURL
 *            要先显示的图片URL
 * @param imgShowURL
 *            <a>标签ID
 */
function showImg(imgShowURL,aId){
	if(imgShowURL != null && imgShowURL.length > 0){
    	$("#" + aId).css({
    		"width":"50",
    		"cursor":"pointer",
    		"color": "#2A2AFF",
    		"display":""
    	});
    	$("#" + aId).click(function(){
    		tanchupic(imgShowURL);
    		});
    }else{
    	$("#" + aId).css("display","none");
    }
}